home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Presentations / Presentations ’92 / PatchWorks Kit / <PatchWorks++> / LoadExtension.c < prev    next >
C/C++ Source or Header  |  1992-05-30  |  2KB  |  110 lines

  1. /*
  2.     LoadExtension.c
  3.     
  4.     Loads an extension into the system heap. Assumes that the extension
  5.     code resource is already in the system heap and locked (has system resource
  6.     bit set). Detaches code if extension loads successfully. Extension should
  7.     throw an exception if unsuccessful.
  8.     
  9.     Part of PatchWorks, the Extension Development Framework.
  10.     
  11.     by Mouse Herrell & Patrick Beard.
  12.     
  13.     Permission is granted to use this source code for any purpose, as long
  14.     as the copyright notice is maintained.
  15.     
  16.     © 1992 Berkeley Systems, Inc.
  17. */
  18.  
  19. #include <ShutDown.h>
  20. #include <Memory.h>
  21. #include <Resources.h>
  22.  
  23. #include "Exceptions.h"
  24. #include "LoadExtension.h"
  25. #include "Patch.h"
  26. #include "Globals.h"
  27.  
  28. /* prototypes. */
  29. void main(void);
  30. OSErr Startup(void);
  31. static pascal void RemoveAtShutDown(void);
  32.  
  33. /* globals. */
  34. short theSystemVersion;
  35. Boolean theCQDFlag;
  36. Handle theExtensionCode;
  37.  
  38. void main()
  39. {
  40.     THz oldZone;
  41.     
  42. #ifdef THINK_C
  43.     InitGlobals();                
  44.     OpenGlobals();
  45.  
  46.     oldZone = GetZone();
  47.     SetZone(SystemZone());
  48. #endif
  49.  
  50. #ifdef applec
  51.     oldZone = GetZone();
  52.     SetZone(SystemZone());
  53.  
  54.     InitGlobals();                
  55.     OpenGlobals();
  56. #endif
  57.     
  58.     // get the handle to our code resource.
  59.     theExtensionCode = GetCodeHandle();
  60.     
  61.     // if everything loaded ok, stay in memory.
  62.     if (Startup() != noErr)
  63.         DestroyGlobals();
  64.     else
  65.         DetachResource(theExtensionCode);
  66.     
  67.     CloseGlobals();
  68.  
  69.     SetZone(oldZone);
  70. }
  71.  
  72. OSErr Startup(void)
  73. {
  74.     SysEnvRec environment;
  75. #ifdef THINK_C
  76.     long oldA5 = InitQD();
  77. #endif
  78.     OSErr result = noErr;
  79.     
  80.     SysEnvirons(curSysEnvVers, &environment);
  81.     theCQDFlag = environment.hasColorQD;
  82.     theSystemVersion = environment.systemVersion;
  83.     
  84.     try {
  85.         Install();
  86.     } catch {
  87.         Remove();
  88.         result = theException;
  89.     }
  90.     
  91. #ifdef THINK_C
  92.     SetA5(oldA5);
  93. #endif
  94.  
  95.     return result;
  96. }
  97.  
  98. void InstallShutDownTask()
  99. {
  100.     ShutDwnInstall((ShutDwnProcPtr)RemoveAtShutDown, sdOnUnmount);
  101. }
  102.  
  103. static pascal void RemoveAtShutDown()
  104. {
  105.     OpenGlobals();
  106.     Remove();
  107.     CloseGlobals();
  108.     DestroyGlobals();
  109. }
  110.